package org.roger.highlighter;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;

public abstract class AbstractHighlighter {

	/**
	 * 获取高亮显示的样式
	 * @return
	 */
	public abstract Formatter getFormatter();

	public abstract Query getQuery();
	
	protected Highlighter getHighlighter(){
		Formatter formatter = getFormatter();
		Query query = getQuery();
		//设置高亮片段分数
		QueryScorer queryScorer = new QueryScorer(query);
		Highlighter highlighter = new Highlighter(formatter,queryScorer);
		//设置片段生成规则
		highlighter.setTextFragmenter(new SimpleSpanFragmenter(queryScorer));
		return highlighter; 
	}
}
